﻿using System;
using System.Collections.Generic;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using System.Xml;

namespace PT.ThueXe
{
    partial class EditThueXe : PortalModuleBase
    {

        #region Private Members

        private int ItemId = Null.NullInteger;

        #endregion

        #region Event Handlers

        protected void Page_Load(System.Object sender, System.EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    ThueXeController objController = new ThueXeController();
                    List<CarTypeInfo> carTypeList = objController.GetCarTypeList();
                    carTypeList.Insert(0, new CarTypeInfo { CarTypeID = 0, TypeName = "----" });
                    this.cboCarType.DataSource = carTypeList;
                    this.cboCarType.DataBind();

                    List<CityInfo> cityList = objController.GetCityList();
                    cityList.Insert(0, new CityInfo { CityID = 0, CityName = "----" });
                    this.cboFromCity.DataSource = cityList;
                    this.cboFromCity.DataBind();

                    this.cboToCity.DataSource = cityList;
                    this.cboToCity.DataBind();
                    /////////////////
                    ThueXeEmail objEmail = objController.GetThueXeEmail();
                    if (objEmail != null)
                    {
                        this.txtMailFrom.Text = objEmail.MailFrom;
                        this.txtMailTo.Text = objEmail.MailTo;
                    }
                    else
                    {
                        this.txtMailFrom.Text = "";
                        this.txtMailTo.Text = "";
                    }
                }

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        protected void _btnOK_Click(System.Object sender, System.EventArgs e)
        {
            this._getData();
            this.grdThueXe.DataBind();
        }

        private void _getData()
        {
            ThueXeController objController = new ThueXeController();
            List<ThueXeInfo> listThueXe = objController.GetThueXes(this.txtName.Text.Trim(), int.Parse(this.cboCarType.SelectedValue), int.Parse(this.cboFromCity.SelectedValue), int.Parse(this.cboToCity.SelectedValue), this.dteFromDate.SelectedDate, this.dteTodate.SelectedDate, this.dteCreatedDate.SelectedDate);
            foreach (ThueXeInfo item in listThueXe)
            {
                if (!string.IsNullOrEmpty(item.DetailInformation))
                {
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(item.DetailInformation);
                    XmlNode xNode = xdoc.SelectSingleNode("//root/item");
                    if (xNode != null)
                    {
                        string strDetail = "<strong>Thông tin xe:</strong><br/>" +
                            "<strong>Tên xe:</strong>" + xNode.Attributes["tenxe"].Value + "<br/>" +
                            "<strong>Hiệu xe:</strong>" + xNode.Attributes["hieuxe"].Value + "<br/>" +
                            "<strong>Loại xe:</strong>" + xNode.Attributes["loaixe"].Value + "<br/>";
                        item.DetailInformation = strDetail;
                    }
                }
            }
            this.grdThueXe.DataSource = listThueXe;
        }

        protected void _btnSave_Click(System.Object sender, System.EventArgs e)
        {
            ThueXeController objController = new ThueXeController();
            objController.AddUpdateThueXeEmail(this.txtMailFrom.Text, this.txtMailTo.Text.Trim());
        }

        protected void _grdThueXe_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            this._getData();
        }
        #endregion

    }
}

